package com.simen.accessible;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.os.Build;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.simen.invoker.Invoker;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

public class MyService extends AccessibilityService {

    private static final String TAG = "MyService";

    @Override
    public void onServiceConnected() {
        Log.d(TAG, "onServiceConnected");
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        final int eventType = event.getEventType();
        String eventText = null;
        switch (eventType) {
            case AccessibilityEvent.TYPE_VIEW_CLICKED:
                eventText = "CLICKED: ";
                break;
            case AccessibilityEvent.TYPE_VIEW_FOCUSED:
                eventText = "Focused: ";
                break;
            case AccessibilityEvent.TYPE_WINDOWS_CHANGED:
                eventText = "WINDOWS_CHANGED: ";
                break;
            case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
                eventText = "WINDOW_CONTENT_CHANGED: ";
                break;
            case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
                eventText = "WINDOW_STATE_CHANGED: ";
                if (!performClick(event, "安装")) {
                    performClick(event, "完成");
                }
                break;
            case AccessibilityEvent.TYPE_VIEW_SCROLLED:
                eventText = "TYPE_VIEW_SCROLLED: ";
                break;
            case AccessibilityEvent.TYPE_VIEW_SELECTED:
                eventText = "TYPE_VIEW_SELECTED: ";
                break;
            default:
                eventText = "Other type event:" + eventType;
                break;
        }

        Log.d(TAG, eventText);
    }

    private boolean performClick(AccessibilityEvent event, String text) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            return true;
        }
        AccessibilityNodeInfo source = getRoot(event.getSource());
        List<AccessibilityNodeInfo> list = source.findAccessibilityNodeInfosByText(text);
        if (null != list && !list.isEmpty()) {
            boolean action_click = false;
            for (AccessibilityNodeInfo info : list) {
                CharSequence content = null;
                if (info != null && info.isVisibleToUser() && (content = info.getText()) != null && content
                        .toString().equals(text)) {
                    List<AccessibilityNodeInfo.AccessibilityAction> actions = info.getActionList();
                    if (actions != null && !actions.isEmpty()) {
                        actions.clear();
                    }
                    info.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    action_click = true;
                }
            }
            if (action_click) return true;
        }
        return false;
    }

    private AccessibilityNodeInfo getRoot(AccessibilityNodeInfo source) {
        if (source == null) return null;
        while (true) {
            AccessibilityNodeInfo next = source.getParent();
            if (next == null) {
                return source;
            }
            source = next;
        }
    }

    @Override
    public void onInterrupt() {

    }

}
